<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{asset('lib/layui-v2.6.3/css/layui.css')}}" media="all">
    <link rel="stylesheet" href="{{asset('css/public.css')}}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">角色名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="不能为空" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">权限分配</label>
        <div class="layui-input-block">
            @foreach($permission as $p)
            <input type="checkbox" name="permissions[{{$p->id}}]" title="{{$p->name}}">
            @endforeach
        </div>
    </div>

    @csrf
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="{{asset('lib/layui-v2.6.3/layui.js')}}" charset="utf-8"></script>
<script src="{{asset('js/lay-config.js?v=2.0.0')}}" charset="utf-8"></script>
<script src="{{asset('js/app.js')}}" charset="utf-8"></script>
<script>
        layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                type: 'POST',
                url: '/api/user/storeRole',
                dataType: 'json',
                data: data.field,
                success:function (res) {
                    if (res.code == 200) {
                        layer.closeAll();
                        parent.location.reload();
                        let iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    } else {
                        layer.open({
                            title: '出问题了'
                            ,content: '网络阻塞，请稍后再试'
                        });
                    }
                },
                //处理422
                error:function (err) {
                    if (err.status == 422) {
                        let json = JSON.parse(err.responseText);
                        json = json.errors;
                        let errorInfo = [];
                        for (let item in json) {
                            for (let i = 0; i < json[item].length; i++) {
                                errorInfo.push(json[item][i]);
                            }
                        }
                        let errStr = errorInfo.join('<br>');
                        layer.open({
                            title: '填写错误'
                            ,content: errStr
                        });
                    }
                    return false;
                },
                complete:function () {

                },
            });
            return false;
        });

    });

</script>
</body>
</html>
